package org.xteam.engine;

import java.awt.Point;

import org.xteam.engine.geom.Matrix;
import org.xteam.engine.geom.Point3D;

public class Camera {

	private Point3D location;
	private Point3D lookAt;
	private double roll;
	
	private Matrix matrix;
	
	private double focal = 30.0;
	private double scaleX = 100.0;
	private double scaleY = 100.0;
	private Point origin;
	
	public Camera(Point3D location, Point3D lookAt, double roll, Point origin, double focal) {
		this.location = location;
		this.lookAt = lookAt;
		this.roll = roll;
		this.origin = origin;
		this.focal = focal;
	}
	
	public Matrix getMatrix() {
		if (matrix == null) {
			buildMatrix();
		}
		return matrix;
	}

	private void buildMatrix() {
		Point3D up = new Point3D(Math.sin(roll), -Math.cos(roll), 0.0);
		Point3D forward = lookAt.substract(location).normalize();
		Point3D right = up.crossProduct(forward).normalize();
		up = right.crossProduct(forward).normalize();
		matrix = new Matrix(new double[][] {
				{ right.x,   right.y,   right.z,   -location.x },
				{ up.x,      up.y,      up.z,      -location.y },
				{ forward.x, forward.y, forward.z, -location.z },
				{ 0.0,       0.0,       0.0,       1.0 }
		});
	}

	public double getFocal() {
		return focal;
	}

	public double getScaleX() {
		return scaleX;
	}

	public double getScaleY() {
		return scaleY;
	}

	public Point getOrigin() {
		return origin;
	}

	public void applyTransform(Matrix matrix) {
		this.matrix = getMatrix().mutiply(matrix);
	}
}
